package com.model.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Level1TableDAO {

	private static final Log log = LogFactory.getLog(Level1TableDAO.class);

	Session session;
	Transaction tx;

	public void persist(Level1Table transientInstance) {
		log.debug("persisting Level1Table instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.persist(transientInstance);
			tx.commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void remove(Level1Table persistentInstance) {
		log.debug("removing Level1Table instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Level1Table merge(Level1Table detachedInstance) {
		log.debug("merging Level1Table instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			Level1Table result = (Level1Table) session
					.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Level1Table findById(Integer id) {
		log.debug("getting Level1Table instance with id: " + id);
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			Level1Table instance = (Level1Table) session.get(
					Level1Table.class, id);
			tx.commit();
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	@SuppressWarnings("unchecked")
	public List<Level1Table> findAll() throws Exception {
		log.debug("getting Level1Table instance ");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
//			/Query query = session.createQuery("select distinct ot from OrderTable ot Join fetch ot.orderItemsTables oit  join fetch oit.childcatalogTable cct join fetch cct.mastercatalogTable where orderstatus <> 'Delivered'");
			Query query = session.createQuery("select distinct l1  from Level1Table l1 left Join fetch l1.level2Tables l2 left join fetch l2.level3Tables  ");
			List<Level1Table> list = query.list();
			tx.commit();
			log.debug("get successful");
			return list;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
			throw ex;
		}
	}
	public static void main(String[] args) throws Exception {
		Level1TableDAO dao=new Level1TableDAO();
		dao.findAll();
	}
}
